/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.laogao.jenkinestestpackage01.demos.web;

import com.laogao.jenkinestestpackage01.demos.entity.TimeRangeQueryVo;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
public class BasicController {

    //          http://127.0.0.1:5678/hello?name=lisi
    //          http://110.40.173.19:5678/hello?name=lisi
    // curl     http://127.0.0.1:5678/hello?name=lisi
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        return "docker部署成功77777 " + name;
    }

    /**
     * http://127.0.0.1:5678/query1?startTime=2024-01-01 00:00:00&endTime=2024-01-31 23:59:59
     *
     * @param startTime
     * @param endTime
     * @return
     */

    // 接收 Query 参数并自动转换为 LocalDateTime
    @GetMapping("/query1")
    public String queryByTimeRange(
            // 开始时间：指定格式为 yyyy-MM-dd HH:mm:ss
            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            // 结束时间：同上
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        // 校验时间范围合法性
        if (startTime.isAfter(endTime)) {
            return "错误：开始时间不能晚于结束时间";
        }
        // 业务逻辑：查询数据...
        return "查询成功，时间范围：" + startTime + " 至 " + endTime;
    }

    /**
     * http://127.0.0.1:5678/query2?startTime=2024-01-01 00:00:00&endTime=2024-01-31 23:59:59
     * 使用QueryVo接收时间范围查询参数
     */

    @GetMapping("/query2")
    public Result queryByTimeRange(TimeRangeQueryVo queryVo) {
        // 校验参数（省略）
        // 构建响应对象（包含LocalDateTime类型）
        Result result = new Result();
        result.setSuccess(true);
        result.setStartTime(queryVo.getStartTime());
        result.setEndTime(queryVo.getEndTime());
        result.setMessage("查询成功");

        return result; // 自动按全局格式序列化时间
    }

    // 响应对象定义
    @Data
    static class Result {
        private boolean success;
        private LocalDateTime startTime;
        private LocalDateTime endTime;
        private String message;
    }
}
